package cn.org.xiaosheng.webclient;

import cn.org.xiaosheng.webclient.utils.WebClientUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author XiaoSheng
 * @date 2023-11-17
 * @dec 使用WebClient作为技术底座
 */
@RestController
public class GetWebClientTest {

    @Resource
    private WebClientUtils webClientUtils;

    @RequestMapping("/webClientGetTest1")
    public String testCreate() {
        Map<String, Object> headers = new HashMap<>();
        headers.put("tenant-id", "1");
        headers.put("Authorization", "Bearer test1");
        headers.put("client-type", "web");
        headers.put("Conten-Type", "x-www-form-urlencoded");
        Map<String, Object> params = new HashMap<>();
        params.put("pageNo", "2");
        params.put("pageSize", "10");
        Mono<String> mono = WebClient.builder()
                .defaultHeader("tenant-id","1")
                .defaultHeader("Authorization", "Bearer test1")
                .defaultHeader("client-type", "web")
                .defaultHeader("Conten-Type", "x-www-form-urlencoded")
                .build()
                //方法调用，WebClient中提供了多种方法
                .method(HttpMethod.GET)
                //请求url
                .uri("http://xxxxx/admin-api/operate/project-list/names?pageNo={pageNo}&pageSize={pageSize}", params)
                //获取响应结果
                .retrieve()
                //将结果转换为指定类型
                .bodyToMono(String.class);
        //block方法返回最终调用结果，block方法是阻塞的
        System.out.println("响应结果：" + mono.block());
        mono.subscribe(result -> System.out.println(result));
        return mono.block();
    }

    @RequestMapping("/webClientGetTest2")
    public String webClientGetTest2() {
        WebClient  webClient  = WebClient.create();
        Mono<String> stringMono = webClient.get()
                .uri("https://www")
                .retrieve()
                .onStatus(HttpStatus::isError, response -> {
                    // 处理错误状态码
                    return Mono.error(new Exception("发生自定义错误。"));
                })
                .bodyToMono(String.class);
        return stringMono.block();
    }

    @RequestMapping("/webClientGetTest3")
    public String webClientGetTest3() {
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("tenant-id", "1");
        headers.add("Authorization", "Bearer test1");
        headers.add("client-type", "web");
        headers.add("Conten-Type", "x-www-form-urlencoded");
        Map<String, Object> params = new HashMap<>();
        params.put("pageNo", "2");
        params.put("pageSize", "10");
        String url = "http://xxxxxxxxxxx/admin-api/operate/project-list/names?pageNo={pageNo}&pageSize={pageSize}";
        JSONObject respData = webClientUtils.getRequestFlux(url, headers, params);
        return respData.toJSONString();
    }

    @RequestMapping("/webClientGetTest4")
    public String webClientGetTest4() {
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("tenant-id", "1");
        headers.add("Authorization", "Bearer test1");
        headers.add("client-type", "web");
        headers.add("Conten-Type", "x-www-form-urlencoded");
        Map<String, Object> params = new HashMap<>();
        params.put("pageNo", "2");
        params.put("pageSize", "10");
        String url = "http://xxxxxx/admin-api/operate/project-list/names?pageNo={pageNo}&pageSize={pageSize}";
        JSONObject respData = webClientUtils.getRequest(url, headers, params);
        return respData.toJSONString();
    }

}
